package com.cuitrip.business.reminder.model;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuitrip.apiservice.UserBusiness;
import com.cuitrip.business.home.IndexActivity;
import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.business.reminder.RemindDialogActivity;
import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.business.user.proxy.UserInfoProxy;
import com.cuitrip.push.model.PushMessageModel;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.cuitrip.util.UIUtil;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.schemeurl.SchemeUrlActivity;
import com.lab.schemeurl.constant.Scheme;
import com.lab.utils.MessageUtils;

import java.lang.ref.WeakReference;

import butterknife.OnClick;

/**
 * Created by ak on 15/11/5.
 */
public abstract class ReminderInfoViewHolder implements ReminderViewHolder<PushMessageModel> {


    IconTextView actionStatus;

    TextView title;

    TextView subTitle;

    TextView dateText;

    TextView serviceName;

    TextView firstAction;

    TextView secondAction;

    ImageView closeAction;



    WeakReference<RemindDialogActivity> mWeakHostActivity;

    @Override
    public int withLayoutRes() {
        return R.layout.remind_info_dialog;
    }

    @Override
    public void build(RemindDialogActivity activity) {
        serviceName = UIUtil.findTextViewById(activity, R.id.service_name);
        actionStatus = UIUtil.findIconTextViewById(activity, R.id.action_status);
        subTitle = UIUtil.findTextViewById(activity, R.id.sub_title);
        title = UIUtil.findTextViewById(activity, R.id.title);
        dateText = UIUtil.findTextViewById(activity, R.id.date_text);
        firstAction = UIUtil.findTextViewById(activity,R.id.first_action);
        secondAction = UIUtil.findTextViewById(activity, R.id.second_action);
        closeAction = UIUtil.findImageViewById(activity, R.id.close_button);
        mWeakHostActivity = new WeakReference<RemindDialogActivity>(activity);
        closeAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        if (UserInfoProxy.getInstance().isTraveller()){
            firstAction.setBackgroundResource(R.drawable.selector_white_corner_orange_line);
            firstAction.setTextColor(ResourceUtil.getThemeColor());
            secondAction.setBackgroundResource(R.drawable.selector_orange_rec_corner);
            secondAction.setTextColor(ResourceUtil.getColor(R.color.ct_white));
        }else{
            firstAction.setBackgroundResource(R.drawable.selector_white_corner_blue_line);
            firstAction.setTextColor(ResourceUtil.getThemeColor());
            secondAction.setBackgroundResource(R.drawable.selector_blue_rec_corner);
            secondAction.setTextColor(ResourceUtil.getColor(R.color.ct_white));
        }


    }

    public void dismiss(){
        Activity activity = mWeakHostActivity.get();
        if (null != activity){
            activity.finish();
        }
    }

    @OnClick(R.id.close_button)
    public void closeButton(){

    }


    public void changeType(final String schemeUrl){
        final CtUserInfo userInfo = LoginInstance.getInstance().getUserInfo();
        final int type = userInfo.isTravel() ? CtUserInfo.USER_FINDER : CtUserInfo.USER_TRAVEL;
        IProxyCallback proxyCallback = new IProxyCallback() {
            @Override
            public boolean onProxyResult(ProxyResult<?> result) {
                CtApiResponse response = (CtApiResponse) result.getData();
                if (response.isResponseNormal()){
                    userInfo.setType(type);
                    LoginInstance.update(mWeakHostActivity.get(), userInfo, true);
                    Intent intent = new Intent(mWeakHostActivity.get(), IndexActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString(SchemeUrlActivity.SchemeUrl, Scheme.CUITRIP.getSchemeName());
                    intent.setData(Uri.parse(schemeUrl));
                    intent.putExtras(bundle);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    mWeakHostActivity.get().startActivity(intent);
                }else{
                    if (!TextUtils.isEmpty(response.msg)) {
                        MessageUtils.showToast(response.msg);
                    }
                }
                return true;
            }
        };
        ApiProxy apiProxy = new ApiProxy(proxyCallback);
        UserBusiness.changeType(apiProxy, type);
    }


}
